<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="uUsername">
      <el-input v-model="dataForm.uUsername" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="管理员分类" prop="uAdminsort">
      <el-input v-model="dataForm.uAdminsort" placeholder="管理员分类"></el-input>
    </el-form-item>
    <el-form-item label="员工姓名" prop="uEmpname">
      <el-input v-model="dataForm.uEmpname" placeholder="员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="uDeptname">
      <el-input v-model="dataForm.uDeptname" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="uCreatdate">
      <el-input v-model="dataForm.uCreatdate" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="授权状态" prop="uAuthorstatus">
      <el-input v-model="dataForm.uAuthorstatus" placeholder="授权状态"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="uPassword">
      <el-input v-model="dataForm.uPassword" placeholder="用户密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uId: 0,
          uUsername: '',
          uAdminsort: '',
          uEmpname: '',
          uDeptname: '',
          uCreatdate: '',
          uAuthorstatus: '',
          uPassword: ''
        },
        dataRule: {
          uUsername: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          uAdminsort: [
            { required: true, message: '管理员分类不能为空', trigger: 'blur' }
          ],
          uEmpname: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
          ],
          uDeptname: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          uCreatdate: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          uAuthorstatus: [
            { required: true, message: '授权状态不能为空', trigger: 'blur' }
          ],
          uPassword: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uId) {
            this.$http({
              url: this.$http.adornUrl(`/com/user/info/${this.dataForm.uId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uUsername = data.user.uUsername
                this.dataForm.uAdminsort = data.user.uAdminsort
                this.dataForm.uEmpname = data.user.uEmpname
                this.dataForm.uDeptname = data.user.uDeptname
                this.dataForm.uCreatdate = data.user.uCreatdate
                this.dataForm.uAuthorstatus = data.user.uAuthorstatus
                this.dataForm.uPassword = data.user.uPassword
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/user/${!this.dataForm.uId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uId': this.dataForm.uId || undefined,
                'uUsername': this.dataForm.uUsername,
                'uAdminsort': this.dataForm.uAdminsort,
                'uEmpname': this.dataForm.uEmpname,
                'uDeptname': this.dataForm.uDeptname,
                'uCreatdate': this.dataForm.uCreatdate,
                'uAuthorstatus': this.dataForm.uAuthorstatus,
                'uPassword': this.dataForm.uPassword
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
